
package com.huizhan.web.controller;
 
import org.springframework.web.bind.annotation.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.*;
import org.springframework.data.domain.Sort.*;
import io.swagger.annotations.*;

import java.math.BigInteger;
import java.util.*;

import com.huizhan.web.dao.*;
import com.huizhan.web.model.*;
import com.huizhan.web.view.*;

/**
 * 通行记录api
 */
 
@Api(tags = "AccessRecord",description = "通行记录")
@RestController
public class AccessRecordController {
    
    @Autowired
    private AccessRecordDao dao;
   
    /**
     * 获取通行记录集合
     * @return api结果
     */
    @ApiOperation(value = "获取通行记录集合")
    @RequestMapping(value =  "/api/AccessRecord/Search/",method = {RequestMethod.POST})
    public ApiResult<List<Report>> Search(@RequestBody PageDate model){
         ApiResult<List<Report>> result=new ApiResult<List<Report>>();
        try 
        {
            List<Report> rList=new ArrayList<Report>();

            List<Object>  res = dao.findByWhere(model.getProjectId(),model.getName(),model.getStartDate().getTime()/1000,model.getEndDate().getTime()/1000,model.getPhone());

            for (Object object : res) {
                Object [] a=(Object[])object;
                Report report=new com.huizhan.web.view.Report();

                report.setNumber((BigInteger)a[0]);
                report.setDoor((String)a[1]);
                report.setTypeName((String)a[2]);
                report.setTime((String)a[3]);
                report.setDocument((BigInteger)a[4]);
                report.setTicket((BigInteger)a[5]);
               rList.add(report);
            }

            result.setData(rList);
        }catch(Exception e){
            result.setHasError(true);
            result.setMessage(e.getMessage());
        }
        return  result;
    }

    /**
     * 根据ID获取通行记录信息
     * @param id 通行记录ID
     * @return api结果
     */
    @ApiOperation(value = "根据ID获取通行记录信息")
    @RequestMapping(value =  "/api/AccessRecord/{id}",method = {RequestMethod.GET})
    public ApiResult<AccessRecord> Get(@PathVariable Long id){
        ApiResult<AccessRecord> result=new ApiResult<AccessRecord>();
        try
        {
            AccessRecord model= dao.getOne(id);
            result.setData(model);
        }catch(Exception e){
            result.setHasError(true);
            result.setMessage(e.getMessage());
        }
        return  result;
    }

    /**
     * 修改通行记录信息
     * @param id 通行记录ID
     * @param model 通行记录信息
     * @return api结果
     */
    @ApiOperation(value = "修改通行记录信息")
    @RequestMapping(value =  "/api/AccessRecord/{id}",method = {RequestMethod.PUT})
    public ApiResult<AccessRecord> Edit(@PathVariable Long id,@RequestBody AccessRecord model){
        ApiResult<AccessRecord> result=new ApiResult<AccessRecord>();
        try
        {
            model.setAccessRecordId(id);
            dao.save(model);
            result.setData(model);
        }catch(Exception e){
            result.setHasError(true);
            result.setMessage(e.getMessage());
        }
        return  result;
    }

    /**
     * 查询证件记录集合
     * @param model 账号信息
     * @return api结果
     */
    @ApiOperation(value = "查询证件通行记录集合")
    @RequestMapping(value =  "/api/AccessRecord/Document/Search",method = {RequestMethod.POST})
    public ApiResult<List<AccessRecord>> Search(@RequestBody PageAccessRecord model){
         ApiResult<List<AccessRecord>> result=new ApiResult<List<AccessRecord>>();
         
        try
        {    
            PageRequest pageable = PageRequest.of(model.getPageIndex(),model.getPageSize(),Direction.DESC,"access_record_Id") ;            
            Page<AccessRecord> page = dao.findByWhere(model.getName(),model.getCode(),model.getProjectId(),model.getDocumentTypeId(),model.getProjectDoorId(),model.getIdNumber(),pageable);
            result.setData(page.getContent());
            result.setTotal(page.getTotalElements());
        }catch(Exception e){
            result.setHasError(true);
            result.setMessage(e.getMessage());
        }
        
        return result;
    }
    
    /**
     * 查询证件记录集合
     * @param model 账号信息
     * @return api结果
     */
    @ApiOperation(value = "查询票通行记录集合")
    @RequestMapping(value =  "/api/AccessRecord/Ticket/Search",method = {RequestMethod.POST})
    public ApiResult<List<AccessRecord>> Search(@RequestBody PageTicketAccessRecord model){
         ApiResult<List<AccessRecord>> result=new ApiResult<List<AccessRecord>>();
         
        try
        {    
            PageRequest pageable = PageRequest.of(model.getPageIndex(),model.getPageSize(),Direction.DESC,"access_record_Id") ;            
            Page<AccessRecord> page = dao.findByWhere(model.getTicketTypeId(),model.getName(),model.getCode(),model.getProjectId(),model.getProjectDoorId(),model.getIdNumber(),pageable);
            result.setData(page.getContent());
            result.setTotal(page.getTotalElements());
        }catch(Exception e){
            result.setHasError(true);
            result.setMessage(e.getMessage());
        }
        
        return result;
    }
    
    /**
     * 添加通行记录信息
     * @param model 通行记录信息
     * @return api结果
     */
    @ApiOperation(value = "添加通行记录信息")
    @RequestMapping(value =  "/api/AccessRecord/",method = {RequestMethod.POST})
    public ApiResult<AccessRecord> Add(@RequestBody AccessRecord model){
         ApiResult<AccessRecord> result=new ApiResult<AccessRecord>();
        try
        {
            model.setCreateDate(new Date());
            dao.save(model);
            result.setData(model);
        }catch(Exception e){
            result.setHasError(true);
            result.setMessage(e.getMessage());
        }
        return result;
    }
    
    /**
     * 删除通行记录信息
     * @param id 通行记录ID
     * @return api结果
     */
    @ApiOperation(value = "删除通行记录信息")
    @RequestMapping(value =  "/api/AccessRecord/{id}",method = {RequestMethod.DELETE})
    public ApiResult<Object> Delete(@PathVariable  Long id){
        ApiResult<Object> result=new ApiResult<Object>();
        try 
        {
            dao.deleteById(id);
        }catch(Exception e){
            result.setHasError(true);
            result.setMessage(e.getMessage());
        }
        return result;
    }
}

